@page "/User/Loans"
@using Rental.Data.Context
@inject RentalContext RentalContext

<Navigation></Navigation>

<AuthorizeView>
    <Authorized>
        <table class="table">
            <thead>
            <tr>
                <th scope="col">#</th>
                <th scope="col">Date Start</th>
                <th scope="col">Date End</th>
                <th scope="col">Total Days</th>
                <th scope="col">License</th>
                <th scope="col">Status</th>
                <th scope="col">Actions</th>
            </tr>
            </thead>
            <tbody>
            @foreach (var loan in _loans)
            {
                <tr>
                    <th scope="row">@loan.Serial</th>
                    <td>@loan.DateStart.ToString("yyyy-MM-dd")</td>
                    <td>@loan.DateEnd.ToString("yyyy-MM-dd")</td>
                    <td>@GetTotalDaysUsed(@loan.DateStart, @loan.DateEnd)</td>
                    <td>@loan.CarLicense</td>
            
                    @if (IsCanceled(@loan.HasBeenCanceled))
                    {
                        <td><span class="badge bg-danger">Canceled</span></td>
                    }
                    else if (IsFuture(@loan.DateStart))
                    {
                        <td><span class="badge bg-primary">Future</span></td>
                    }
                    else if (IsActive(@loan.DateEnd))
                    {
                        <td><span class="badge bg-success">Active</span></td>
                    }
                    else
                    {
                        <td><span class="badge bg-secondary">Inactive</span></td>
                    }
            
                    <td><button>Cancel</button><button>Info.</button></td>
                </tr>
            }
            </tbody>
        </table>
    </Authorized>
    <NotAuthorized>
        <MessageNotAuthorized/>
    </NotAuthorized>
</AuthorizeView>


<Footer></Footer>

@code
{
    [Parameter]
    public string User { get; set; }
    
    [CascadingParameter]
    private Task<AuthenticationState> AuthenticationStateTask { get; set; }
    
    private List<Rental.Data.Loan> _loans;

    /// Calculate difference between two dates (number of days).
    /// <returns>The days of difference between the dates</returns>
    private int GetTotalDaysUsed(DateTime start, DateTime end)
    {
        // Reference: https://stackoverflow.com/a/1607352
        return (end - start).Days;
    }
    
    private bool IsCanceled(string isCanceled)
    {
        if (isCanceled.Equals("TRUE"))
        {
            return true;
        }
        
        return false;
    }
    
    private bool IsFuture(DateTime start)
    {
        if (start.CompareTo(DateTime.Today) > 0)
        {
            return true;
        }

        return false;
    }

    private bool IsActive(DateTime end)
    {
        if (end.CompareTo(DateTime.Today) >= 0)
        {
            return true;
        }

        return false;
    }

    protected override async Task OnInitializedAsync()
    {
        var authState = await AuthenticationStateTask;
        var user = authState.User;

        if (user.Identity.IsAuthenticated)
        {
            _loans = await RentalContext.GetLoansByUser(user.Identity.Name);            
        }
    }
}